package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysRoleRelateDAO;
import com.ourteam.system.dao.SysRoleRelate;
import com.ourteam.system.dao.SysRoleRelateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysRoleRelateDAOJdbcImpl extends AbstractSpringDAO
    implements ISysRoleRelateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, RelatedSysRoleId, Remarks, Status, SysRoleGroupId,
            RelatedSysRoleCode, SysRoleName, SysRoleCode, SysRoleType,
            SysRoleDomainId, SysDomainCode, SysDomainName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.RelatedSysRoleId,
            _DataObjectProperty.Remarks, _DataObjectProperty.Status,
            _DataObjectProperty.SysRoleGroupId,
            _DataObjectProperty.RelatedSysRoleCode,
            _DataObjectProperty.SysRoleName, _DataObjectProperty.SysRoleCode,
            _DataObjectProperty.SysRoleType, _DataObjectProperty.SysRoleDomainId,
            _DataObjectProperty.SysDomainCode, _DataObjectProperty.SysDomainName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysRoleRelate, SysRoleRelateDAOQueryBean> daoHelper;

    public AbstractSysRoleRelateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysRoleRelate, SysRoleRelateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysRoleDAO.EntityName)
                                .append(" sysRoleA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.Id,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRoleA."));
            relatedJoinSQLBuffer.append(" = ").append(RelatedSysRoleId);

            relatedJoinSQLBuffer.append(" AND ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.RoleType,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRoleA.")).append(" ").append("= 'ROLE' ");

            relatedJoinSQLBuffer.append(" AND ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.Status,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRoleA.")).append(" ").append("= 'active' ");

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysDomainDAO.EntityName)
                                .append(" sysDomianA ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysDomainDAO.Id,
                    com.ourteam.system.dao.ISysDomainDAO.EntityName + ".",
                    "sysDomianA."));
            relatedJoinSQLBuffer.append(" = ").append(SysRoleDomainId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysRoleRelate
         * @throws Exception
         */
    public boolean checkIsUnique(SysRoleRelate aSysRoleRelate)
        throws Exception {
        if (aSysRoleRelate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysRoleRelate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysRoleRelate
     * @throws Exception
     */
    public void insert(SysRoleRelate aSysRoleRelate) throws Exception {
        super.doInsert(aSysRoleRelate);
    }

    /**
         * Batch Insert Datas
         * @param aSysRoleRelate
         * @throws Exception
         */
    public void batchInsert(SysRoleRelate[] aSysRoleRelates)
        throws Exception {
        super.doBatchInsert(aSysRoleRelates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysRoleRelateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysRoleRelate aSysRoleRelate) throws Exception {
        delete(aSysRoleRelate.getId());
    }

    /**
     * Update Data Selective
     * @param aSysRoleRelate
     * @throws Exception
     */
    public void update(SysRoleRelate aSysRoleRelate) throws Exception {
        if (aSysRoleRelate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysRoleRelate);

        SysRoleRelate tempObj = findById(aSysRoleRelate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysRoleRelate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysRoleRelates[]
     * @throws Exception
     */
    public void batchUpdate(SysRoleRelate[] aSysRoleRelates)
        throws Exception {
        super.doBatchUpdate(aSysRoleRelates);
    }

    /**
         * Update Data By Condictions
         * @param aSysRoleRelate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysRoleRelate aSysRoleRelate,
        SysRoleRelateDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysRoleRelate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysRoleRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysRoleRelate aSysRoleRelate)
        throws Exception {
        if (aSysRoleRelate.getRemarks() == null) {
            throw new Exception(
                "The SysRoleRelate.remarks value is not allow NULL");
        }

        if (aSysRoleRelate.getStatus() == null) {
            throw new Exception(
                "The SysRoleRelate.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysRoleRelate findById(final long id) throws Exception {
        SysRoleRelateDAOQueryBean queryBean = new SysRoleRelateDAOQueryBean();

        SysRoleRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysRoleRelate[] SysRoleRelates = query(queryBean);

        if (SysRoleRelates.length == 0) {
            return null;
        } else {
            return SysRoleRelates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysRoleRelate[]
         * @throws Exception
         */
    public SysRoleRelate[] query(SysRoleRelateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysRoleRelateRowMapper());

        return (SysRoleRelate[]) result.toArray(new SysRoleRelate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysRoleRelateDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysRoleRelateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysRoleRelate[] aSysRoleRelates,
        SysRoleRelateDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysRoleRelates, queryBean);
    }

    public void synchronize(SysRoleRelate[] aSysRoleRelates,
        SysRoleRelateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysRoleRelate> callBack)
        throws Exception {
        daoHelper.synchronize(aSysRoleRelates, queryBean, callBack);
    }

    public static class JdbcSysRoleRelateRowMapper implements RowMapper<SysRoleRelate> {
        public SysRoleRelate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysRoleRelate object = new SysRoleRelate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object relatedSysRoleId = null;

                relatedSysRoleId = resultSet.getObject(columnIndex++);

                object.setRelatedSysRoleIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedSysRoleId != null) {
                    setBeanValue(object, _DataObjectProperty.RelatedSysRoleId,
                        relatedSysRoleId);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sysRoleGroupId = null;

                sysRoleGroupId = resultSet.getObject(columnIndex++);

                object.setSysRoleGroupIdNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleGroupId != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleGroupId,
                        sysRoleGroupId);
                }

                Object relatedSysRoleCode = null;

                relatedSysRoleCode = resultSet.getObject(columnIndex++);

                object.setRelatedSysRoleCodeNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedSysRoleCode != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedSysRoleCode,
                        relatedSysRoleCode);
                }

                Object sysRoleName = null;

                sysRoleName = resultSet.getObject(columnIndex++);

                object.setSysRoleNameNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleName != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleName,
                        sysRoleName);
                }

                Object sysRoleCode = null;

                sysRoleCode = resultSet.getObject(columnIndex++);

                object.setSysRoleCodeNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleCode != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleCode,
                        sysRoleCode);
                }

                Object sysRoleType = null;

                sysRoleType = resultSet.getObject(columnIndex++);

                object.setSysRoleTypeNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleType != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleType,
                        sysRoleType);
                }

                Object sysRoleDomainId = null;

                sysRoleDomainId = resultSet.getObject(columnIndex++);

                object.setSysRoleDomainIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (sysRoleDomainId != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleDomainId,
                        sysRoleDomainId);
                }

                Object sysDomainCode = null;

                sysDomainCode = resultSet.getObject(columnIndex++);

                object.setSysDomainCodeNull(resultSet.getBoolean(columnIndex++));

                if (sysDomainCode != null) {
                    setBeanValue(object, _DataObjectProperty.SysDomainCode,
                        sysDomainCode);
                }

                Object sysDomainName = null;

                sysDomainName = resultSet.getObject(columnIndex++);

                object.setSysDomainNameNull(resultSet.getBoolean(columnIndex++));

                if (sysDomainName != null) {
                    setBeanValue(object, _DataObjectProperty.SysDomainName,
                        sysDomainName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
